/**********************************************************
 * Filename= i2cTests.cpp
 * Project: i2cTests
 * Description: Unit tests for i2cNet project shared library
 */
#include <iostream>
#include <string>
#include "i2cNet.h"
#include "string.h"

void printHelp()
{
    std::cerr << "i2cTests -n <network name>." << std::endl;
}

int main(int argc, const char** argv)
{
    std::string netName="";
    for(int i=1;i<argc;i++)
    {
	std::string param = argv[i];
	if(param=="--help")
        {
            printHelp();
            return 0;
        }
        else if(param=="--rev")
        {
            i2cNet::printRevisionInfo(std::cout);
        }
        else if(param=="-n" || param == "-net")
        {
            i++;
            if(i<argc)
            {
                netName=argv[i];
            }
            else
            {
                std::cerr << "No Network name following -n or -net argument." << std::endl;
                printHelp();
                return 1;
            }
        }
        else
        {
            std::cerr << "Unknown Argument " << param << "." << std::endl;
            printHelp();
            return 1;
        }
    }
    if(netName=="")
    {
        std::cerr << "I2C Network not specified." << std::endl;
        return 1;
    }
    i2cNet network(netName);
    if(network.connect()!=i2cNet::SUCCESS)
    {
        std::cerr << "Failed to connect to i2c Network " << netName << "." << std::endl;
    }
    else if(!network.connected())
    {
        std::cerr <<"i2c Network reporting connected status after disconnect." << std::endl;
    }
    i2cDevice compass("compass",0x60);
    if(network.addDevice(compass)!=i2cNet::SUCCESS)
    {
        std::cerr << "Failed to add compass device to i2c Network." << std::endl;
    }

    if(network.addDevice(compass)==i2cNet::SUCCESS)
    {
        std::cerr << "i2c Network allowed compass device to be added twice." << std::endl;
    }

    i2cDevice robostix("robostix",0x0b);
    if(network.addDevice(robostix)!=i2cNet::SUCCESS)
    {
        std::cerr << "Failed to add robostix device to i2c Network." << std::endl;
    }

    if(network.addDevice(robostix)==i2cNet::SUCCESS)
    {
        std::cerr << "i2c Network allowed robostix device to be added twice." << std::endl;
    }

    if(network.disconnect()!=i2cNet::SUCCESS)
    {
        std::cerr << "i2c Network failed to disconnect." << std::endl;
    }
    else if(network.connected())
    {
        std::cerr <<"i2c Network reporting connected status after disconnect." << std::endl;
    }

    if(network.removeDevice(compass)!=i2cNet::SUCCESS)
    {
        std::cerr <<"i2c Network Failed to remove compass device." << std::endl;
    }

    if(network.removeDevice(compass)==i2cNet::SUCCESS)
    {
        std::cerr <<"i2c Network removed compass already removed." << std::endl;
    }
    if(network.removeDevice(robostix.name())!=i2cNet::SUCCESS)
    {
        std::cerr <<"i2c Network Failed to remove robostsix device." << std::endl;
    }

    if(network.removeDevice(robostix.name())==i2cNet::SUCCESS)
    {
        std::cerr <<"i2c Network removed robostix already removed." << std::endl;
    }
    return 0;
}
